//
//  JackStringExtension.swift
//  SlowTrajectory
//
//  Created by iOS-Czz on 2023/10/23.
//

import Foundation
extension String {
    subscript (i:Int)->String{
        let startIndex = self.index(self.startIndex, offsetBy: i)
        let endIndex = self.index(startIndex, offsetBy: 1)
        return String(self[startIndex..<endIndex])
    }
    subscript (r: Range<Int>) -> String {
        get {
            let startIndex = self.index(self.startIndex, offsetBy: r.lowerBound)
            let endIndex = self.index(self.startIndex, offsetBy: r.upperBound)
            return String(self[startIndex..<endIndex])
        }
    }
    subscript (index:Int , length:Int) -> String {
        get {
            let startIndex = self.index(self.startIndex, offsetBy: index)
            let endIndex = self.index(startIndex, offsetBy: length)
            return String(self[startIndex..<endIndex])
        }
    }
    func substring(to:Int) -> String{
        return self[0..<to]
    }
    func substring(from:Int) -> String{
        return self[from..<self.count]
    }
    func toDate(format: String) -> Date? {
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = format
        return dateFormatter.date(from: self)
    }
    func dayOfWeekEnglish() -> String? {
            let dateFormatter = DateFormatter()
            dateFormatter.dateFormat = "yyyy-MM-dd" // 根据您的日期格式进行调整
            dateFormatter.locale = Locale(identifier: "en_US_POSIX")
            
            if let date = dateFormatter.date(from: self) {
                let calendar = Calendar(identifier: .gregorian)
                let weekday = calendar.component(.weekday, from: date)
                switch weekday {
                case 1:
                    return "星期日"
                case 2:
                    return "星期一"
                case 3:
                    return "星期二"
                case 4:
                    return "星期三"
                case 5:
                    return "星期四"
                case 6:
                    return "星期五"
                case 7:
                    return "星期六"
                default:
                    return nil
                }
            }
            return nil
        }
    
    func daysAgoString() -> String? {
            let dateFormatter = DateFormatter()
            dateFormatter.dateFormat = "yyyy-MM-dd" // 根据您的日期格式进行调整

            if let date = dateFormatter.date(from: self) {
                let calendar = Calendar.current
                let currentDate = Date()

                if let daysAgo = calendar.dateComponents([.day], from: date, to: currentDate).day {
//                    if daysAgo == 0 {
//                        return "today"
//                    } else if daysAgo == 1 {
//                        return "1 day ago"
//                    } else {
                        return "\(daysAgo) 天前"
//                    }
                }
            }

            return nil
        }
    
    static func calculateNextReminderDate(from date: String, withWay way: Int) -> (nextReminderDate: String, daysUntilReminder: Int) {
            let dateFormatter = DateFormatter()
            dateFormatter.dateFormat = "yyyy-MM-dd"
            
            if let initialDate = dateFormatter.date(from: date) {
                let calendar = Calendar.current
                var nextReminderDate: Date?
                
                if way == 0 {
                    // 以年为周期，下一年的同一日期
                    let year = calendar.component(.year, from: initialDate) + 1
                    nextReminderDate = calendar.date(from: DateComponents(year: year, month: calendar.component(.month, from: initialDate), day: calendar.component(.day, from: initialDate)))
                } else if way == 1 {
                    // 以月为周期，下一个月的同一日期
                    let nextMonthDate = calendar.date(byAdding: .month, value: 1, to: initialDate)
                    let year = calendar.component(.year, from: nextMonthDate!)
                    let month = calendar.component(.month, from: nextMonthDate!)
                    nextReminderDate = calendar.date(from: DateComponents(year: year, month: month, day: calendar.component(.day, from: initialDate)))
                } else if way == 2 {
                    // 以周为周期
                    let daysInAWeek = 7
                    let currentDate = Date()
                    var nextDateComponents = DateComponents()
                    nextDateComponents.day = 7
                    nextReminderDate = calendar.date(byAdding: nextDateComponents, to: initialDate)
                    
                    // 如果下一个周期的开始日期在当前日期之前，再次加一个周期
                    while nextReminderDate! <= currentDate {
                        nextReminderDate = calendar.date(byAdding: nextDateComponents, to: nextReminderDate!)
                    }
                }
                
                if let nextReminderDate = nextReminderDate {
                    let daysUntilReminder = calendar.dateComponents([.day], from: Date(), to: nextReminderDate).day ?? 0
                    let nextReminderDateString = dateFormatter.string(from: nextReminderDate)
                    return (nextReminderDateString, daysUntilReminder)
                }
            }
            
            return ("", 0) // 处理日期解析失败或未指定有效的“way”值
        }
}
extension NSMutableAttributedString {
    func resizeSubstring(_ substring: String, with font: UIFont) {
        let range = (self.string as NSString).range(of: substring)
        
        if range.location != NSNotFound {
            self.addAttribute(NSAttributedString.Key.font, value: font, range: range)
        }
    }
}
